﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Desktop
{
    public partial class VExpender : UserControl
    {
        public enum ExpanderStyleEnum
        {
            Right, Left, Top, Bottom
        }

        private bool drawBorderAll = false;
        public bool DrawBorderAll
        {
            get
            {
                return drawBorderAll;
            }
            set
            {
                drawBorderAll = value;
            }

        }

        public VExpender()
        {
            InitializeComponent();
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
           // SetLayout(_dock);
        }

        
        private ExpanderStyleEnum _dock = ExpanderStyleEnum.Right;
        private bool _isExpanded = true;
        private double _width = 180;
        private double _height = 80;


        public new UIElement Content
        {

            get
            {

                return this.border.Child;
            }

            set
            {
                this.border.Child = value;
            }
        }

        public UIElement Child
        {

            get
            {
                return border.Child;

            }
            set
            {
                border.Child = value;
            }
        }


        public static readonly DependencyProperty ExpanderStyleProperty = DependencyProperty.Register("ExpanderStyle",
           typeof(ExpanderStyleEnum), typeof(VExpender), new PropertyMetadata(ExpanderStyleEnum.Right, new PropertyChangedCallback(OnExpanderStyleChanged)));

        public ExpanderStyleEnum ExpanderStyle
        {
            get { return (ExpanderStyleEnum)GetValue(ExpanderStyleProperty); }
            set { SetValue(ExpanderStyleProperty, value); }
        }

        public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded",
          typeof(bool), typeof(VExpender), new PropertyMetadata(true, new PropertyChangedCallback(OnIsExpandedPropertyChanged)));

        public bool IsExpanded
        {
            get { return (bool)GetValue(IsExpandedProperty); }
            set { SetValue(IsExpandedProperty, value); }
        }


        public new  double Width
        {
            get
            {
                return _width;
            }
            set
            {
                _width = value;
                SetExpanded(_isExpanded);
            }
        }

        public new double Height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
                SetExpanded(_isExpanded);
            }
        }

        private static void OnExpanderStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((VExpender)d).OnExpanderStyleChanged((ExpanderStyleEnum)e.NewValue);
        }

        private void OnExpanderStyleChanged(ExpanderStyleEnum o)
        {
            //_dock = o;
            SetLayout(o);
        }

        private void SetLayout(ExpanderStyleEnum style)
        {
            _dock = style;

            switch (style)
            {
                case ExpanderStyleEnum.Right:


                    LayoutRoot.ColumnDefinitions[0].Width = GridLength.Auto;
                    LayoutRoot.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
                    LayoutRoot.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
                    LayoutRoot.RowDefinitions[1].Height = GridLength.Auto; ;


                    borderArrow.Height = 45;
                    borderArrow.Width = 15;

                    //sbCollapse.SetValue("FrameworkElement.Width

                      Storyboard.SetTargetProperty(sbCollapse.Children[0],new PropertyPath(Grid.WidthProperty));
                      Storyboard.SetTargetProperty(sbExpand.Children[0], new PropertyPath(Grid.WidthProperty));
                      ((DoubleAnimation)sbExpand.Children[2]).To = 90;
                      ((DoubleAnimation)sbCollapse.Children[2]).To = -90;

                      border.Background = this.Resources["VerticalGradient"] as Brush;

                      border.BorderThickness = new Thickness(1, 1, drawBorderAll ? 1 : 0, 1);
                      border.CornerRadius = new CornerRadius(10, drawBorderAll? 10 :0, drawBorderAll?10:0, 10);


                      borderArrow.BorderThickness = new Thickness(1, 1, 0, 1);
                      borderArrow.CornerRadius = new CornerRadius(10, 0, 0,10);
                      borderArrow.Margin = new Thickness(0, 0, -1, 0);

                      //LayoutRoot.Width = _width;


                    Grid.SetColumn(border, 1);
                    Grid.SetRow(border, 0);

                    Grid.SetColumn(borderArrow, 0);
                    Grid.SetRow(borderArrow, 0);

                   // ((CompositeTransform)path.RenderTransform).ScaleX = -1;
                    if (_isExpanded)
                    {
                        ((CompositeTransform)path.RenderTransform).Rotation = 90;
                        border.Opacity = 1;
                    }
                    else
                    {
                        ((CompositeTransform)path.RenderTransform).Rotation = -90;
                        border.Opacity = 0;
                    }

                    break;
                case ExpanderStyleEnum.Top:


                    LayoutRoot.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                    LayoutRoot.ColumnDefinitions[1].Width = GridLength.Auto;
                    LayoutRoot.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
                    LayoutRoot.RowDefinitions[1].Height = GridLength.Auto; ;

                    borderArrow.Height = 15;
                    borderArrow.Width = 45;


                    Storyboard.SetTargetProperty(sbCollapse.Children[0],new PropertyPath(Grid.HeightProperty));
                    Storyboard.SetTargetProperty(sbExpand.Children[0], new PropertyPath(Grid.HeightProperty));
                     ((DoubleAnimation)sbExpand.Children[2]).To = 0;
                      ((DoubleAnimation)sbCollapse.Children[2]).To = 180;
                    

                    border.Background = this.Resources["HoriziontalGradient"] as Brush;

                   //   border.ClearValue(Border.CornerRadiusProperty);
                    border.CornerRadius = new CornerRadius(drawBorderAll ? 10 : 0, drawBorderAll ? 10 : 0, 10, 10);
                    border.BorderThickness = new Thickness(1, drawBorderAll ? 1 : 0, 1, 1);

                    borderArrow.BorderThickness = new Thickness(1, 0, 1, 1);
                    borderArrow.CornerRadius = new CornerRadius(0, 0, 10,10);
                    borderArrow.Margin = new Thickness(0, -1, 0, 0);

                     LayoutRoot.Width = Double.NaN;

                     LayoutRoot.Height = _height;
                     if (!_isExpanded)
                     {
                         LayoutRoot.Height = 15;
                     }
                   

                    Grid.SetColumn(border, 0);
                    Grid.SetRow(border, 0);

                    Grid.SetColumn(borderArrow, 0);
                    Grid.SetRow(borderArrow, 1);

                  //  ((CompositeTransform)path.RenderTransform).ScaleX = -1;
                    if (_isExpanded)
                    {
                        ((CompositeTransform)path.RenderTransform).Rotation = 0;
                        border.Opacity = 1;
                    }
                    else
                    {
                        ((CompositeTransform)path.RenderTransform).Rotation = 180;
                        border.Opacity = 0;
                    }

                    break;
                case ExpanderStyleEnum.Left:

                    LayoutRoot.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                    LayoutRoot.ColumnDefinitions[1].Width = GridLength.Auto;
                    LayoutRoot.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
                    LayoutRoot.RowDefinitions[1].Height = GridLength.Auto; ;


                    borderArrow.Height = 45;
                    borderArrow.Width = 15;

                    
                    Storyboard.SetTargetProperty(sbCollapse.Children[0],new PropertyPath(Grid.WidthProperty));
                    Storyboard.SetTargetProperty(sbExpand.Children[0], new PropertyPath(Grid.WidthProperty));

                     ((DoubleAnimation)sbExpand.Children[2]).To = -90;
                      ((DoubleAnimation)sbCollapse.Children[2]).To = 90;


                    border.Background = this.Resources["VerticalGradient"] as Brush;

                   // border.ClearValue(Border.CornerRadiusProperty);
                    border.CornerRadius = new CornerRadius(drawBorderAll ? 10 : 0, 10, 10, drawBorderAll ? 10 : 0);
                    border.BorderThickness = new Thickness(drawBorderAll ? 1 : 0, 1, 1, 1);
                   
                    borderArrow.BorderThickness = new Thickness(0, 1, 1, 1);
                    
                    borderArrow.CornerRadius = new CornerRadius(0, 10, 10, 0);
                    borderArrow.Margin = new Thickness(-1, 0, 0, 0);

                      //LayoutRoot.Width = _width;

                      //LayoutRoot.Height = double.NaN;

                     

                    Grid.SetColumn(border, 0);
                    Grid.SetRow(border, 0);

                    Grid.SetColumn(borderArrow, 1);
                    Grid.SetRow(borderArrow, 0);

                    //((CompositeTransform)path.RenderTransform).ScaleX = -1;
                    if (_isExpanded)
                    {
                        ((CompositeTransform)path.RenderTransform).Rotation = -90;
                        border.Opacity = 1;
                    }
                    else
                    {
                        ((CompositeTransform)path.RenderTransform).Rotation = 90;
                        border.Opacity = 0;
                    }

                    break;
                default:
                    break;


                    border.OnApplyTemplate();

            }
        }


        private static void OnIsExpandedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((VExpender)d).OnIsExpandedPropertyChanged((bool)e.NewValue);
        }

        private void OnIsExpandedPropertyChanged(bool isExpand)
        {
            SetExpanded(isExpand);
        }

        private void SetExpanded(bool isExpand)
        {
            _isExpanded = isExpand;
            LayoutRoot.Width = Double.NaN;
            if (_isExpanded)
            {
                
                if (_dock == ExpanderStyleEnum.Left || _dock == ExpanderStyleEnum.Right)
                {
                    LayoutRoot.Width = _width;
                    LayoutRoot.Height = Double.NaN;
                }
                else
                {
                    LayoutRoot.Height = _height;
                   
                }
            }
            else
            {
                if (_dock == ExpanderStyleEnum.Left || _dock == ExpanderStyleEnum.Right)
                {
                    LayoutRoot.Width = 15;
                }
                else
                {
                    LayoutRoot.Height = 15;
                }
            }
            SetLayout(_dock);
        }


        //    if (isRightPane)
        //    {
        //        LayoutRoot.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
        //        LayoutRoot.ColumnDefinitions[1].Width = GridLength.Auto;
        //        Grid.SetColumn(grid, 1);
        //        ((CompositeTransform)path.RenderTransform).ScaleX = -1;
        //        if (_isExpanded)
        //        {
        //            ((CompositeTransform)path.RenderTransform).Rotation = 0;
        //            border.Opacity = 1;
        //        }
        //        else
        //        {
        //            ((CompositeTransform)path.RenderTransform).Rotation = 180;
        //            border.Opacity = 0;
        //        }
        //    }
        //    else
        //    {
        //        LayoutRoot.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
        //        LayoutRoot.ColumnDefinitions[0].Width = GridLength.Auto;
        //        Grid.SetColumn(grid, 0);
        //        ((CompositeTransform)path.RenderTransform).ScaleX = 1;
        //        if (_isExpanded)
        //        {
        //            ((CompositeTransform)path.RenderTransform).Rotation = 0;
        //            border.Opacity = 1;
        //        }
        //        else
        //        {
        //            ((CompositeTransform)path.RenderTransform).Rotation = 180;
        //            border.Opacity = 0;
        //        }
        //    }
        //}



        private void SetStoryBoardChildren()
        {
            ((DoubleAnimation)(sbExpand.Children[0])).To = _width;
        }

        private void grid_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

            if (_isExpanded)
            {
                sbCollapse.Begin();
            }
            else
            {
                if (ExpanderStyle == ExpanderStyleEnum.Left || ExpanderStyle == ExpanderStyleEnum.Right)
                {

                    if (((DoubleAnimation)sbExpand.Children[0]).To == null)
                    {
                        ((DoubleAnimation)sbExpand.Children[0]).To = _width;
                    }
                }
                else
                {

                    if (((DoubleAnimation)sbExpand.Children[0]).To == null)
                    {
                        ((DoubleAnimation)sbExpand.Children[0]).To = _height;
                    }
                }

                //if (_isRightPane)
                //{

                //}


                sbExpand.Begin();
            }
           IsExpanded = ! IsExpanded;
        }

        private void grid_MouseEnter(object sender, MouseEventArgs e)
        {
            ToggleMenu();
            // VisualStateManager.GoToState(this, "MouseOver", true);
        }

        private void grid_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseLeave", true);
        }

        private void grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //VisualStateManager.GoToState(this, "MousePress", true);
        }

        private void ToggleMenu()
        {
            if (_isExpanded)
            {
                sbCollapse.Begin();
            }
            else
            {
                if (ExpanderStyle == ExpanderStyleEnum.Left || ExpanderStyle == ExpanderStyleEnum.Right)
                {

                    if (((DoubleAnimation)sbExpand.Children[0]).To == null)
                    {
                        ((DoubleAnimation)sbExpand.Children[0]).To = _width;
                    }
                }
                else
                {

                    if (((DoubleAnimation)sbExpand.Children[0]).To == null)
                    {
                        ((DoubleAnimation)sbExpand.Children[0]).To = _height;
                    }
                }

                //if (_isRightPane)
                //{

                //}


                sbExpand.Begin();
            }
            IsExpanded = !IsExpanded;
        }
    }
}
